package com.zhoujing.adapter;

import java.util.ArrayList;
import java.util.List;

import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.zhongxin.wymovieticket.R;
import com.zhoujing.Entity.Cinema;
import com.zhoujing.Entity.District;

public class MeAdapter extends BaseAdapter implements SectionIndexer,
        StickyListHeadersAdapter {

	
	 private LayoutInflater inflater;
	 private List<Cinema> list;
	 private List<District> districts;
	 private int[] sectionIndices;
	 private String[] sectionHeaders;
	 public MeAdapter(Context context,List<Cinema> list,List<District> districts) {
	        inflater = LayoutInflater.from(context);
	        this.list=list;
	        this.districts=districts;
	        sectionIndices = getSectionIndices();  
	        sectionHeaders = getSectionHeaders(); 
	        
	        
	    }
	 private int[] getSectionIndices() {  
	        List<Integer> sectionIndices = new ArrayList<Integer>();  
	        String lastCreateDate =list.get(0).getDistrictId();  
	        sectionIndices.add(0);  
	        for (int i = 1; i < list.size(); i++) {  
	            String createDate =list.get(i).getDistrictId();  
	            if (!createDate.equals(lastCreateDate)) {  
	                lastCreateDate = createDate;  
	                sectionIndices.add(i);  
	            }  
	        }  
	        int[] sections = new int[sectionIndices.size()];  
	        for (int i = 0; i < sectionIndices.size(); i++) {  
	            sections[i] = sectionIndices.get(i);  
	        }  
	        return sections;  
	    }  
	      
	    private String[] getSectionHeaders() {  
	        String[] sectionHeaders = new String[sectionIndices.length];  
	        for (int i = 0; i < sectionIndices.length; i++) {  
	            sectionHeaders[i] = list.get(sectionIndices[i]).getDistrictId(); 
	        }  
	        return sectionHeaders;  
	    }  
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
		
	}

	@Override
	 public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.fragment_cinema_listview_item, parent, false);
            holder.txtTitle=(TextView) convertView.findViewById(R.id.fragment_cinema_name);
            holder.txtTejia=(TextView) convertView.findViewById(R.id.fragment_cinema_tejia);
            holder.txtImax=(TextView) convertView.findViewById(R.id.fragment_cinema_imax);
            holder.txtMark=(TextView) convertView.findViewById(R.id.fragment_cinema_mark);
            holder.txtLocation=(TextView) convertView.findViewById(R.id.fragment_cinema_locationname);
            holder.txtDistance=(TextView) convertView.findViewById(R.id.fragment_cinema_distance);
            holder.txtChangci=(TextView) convertView.findViewById(R.id.fragment_cinema_changci);
            holder.txtFuhao=(TextView) convertView.findViewById(R.id.fragment_cinema_fuhao);
            holder.txtPrice=(TextView) convertView.findViewById(R.id.fragment_cinema_price);
            holder.txtSeat=(ImageView) convertView.findViewById(R.id.fragment_cinema_seat);
            holder.txtQuan=(ImageView) convertView.findViewById(R.id.fragment_cinema_quan);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String title=list.get(position).getName();
        int isDiscoun=list.get(position).getIsDiscount();
        int isMax=list.get(position).getIsImaxSupport();
        String mark=list.get(position).getGrade();
        String location=list.get(position).getCircleName();
        String changci=list.get(position).getScreenings();
        String price=list.get(position).getLowPrice()+"起";
        int seat=list.get(position).getIsSeatSupport();
        int quan=list.get(position).getIsCouponSupport();
        Log.i("------------meadapter", "====================="+location);
	    holder.txtTitle.setText(title);
		
        if (isDiscoun==0) {
			holder.txtTejia.setVisibility(View.GONE);
		}else {
			holder.txtTejia.setVisibility(View.VISIBLE);
		}
        if (isMax==0) {
			holder.txtImax.setVisibility(View.GONE);
		}else {
			holder.txtImax.setVisibility(View.VISIBLE);
		}
        holder.txtMark.setText(mark);
        if (location.equals("null")) {
        	holder.txtLocation.setVisibility(View.GONE);
		}else {
			holder.txtLocation.setText(location);
		}
        holder.txtChangci.setText(changci);
        if (changci.equals("今天暂无影讯排期")) {
			holder.txtFuhao.setVisibility(View.INVISIBLE);
			holder.txtPrice.setVisibility(View.INVISIBLE);
		}else {
			holder.txtPrice.setText(price);
			holder.txtFuhao.setVisibility(View.VISIBLE);
			holder.txtPrice.setVisibility(View.VISIBLE);
		}
        if (seat==0) {
			holder.txtSeat.setVisibility(View.INVISIBLE);
		}
        else {
        	holder.txtSeat.setVisibility(View.VISIBLE);
		}
        if (quan==0) {
			holder.txtQuan.setVisibility(View.INVISIBLE);
		}else {
			holder.txtQuan.setVisibility(View.VISIBLE);
		}
        return convertView;
    }



	 public View getHeaderView(int position, View convertView, ViewGroup parent) {
        HeaderViewHolder holder;
        if (convertView == null) {
            holder = new HeaderViewHolder();
            convertView = inflater.inflate(R.layout.header, parent, false);
            holder.text = (TextView) convertView.findViewById(R.id.textView1);
            convertView.setTag(holder);
        } else {
            holder = (HeaderViewHolder) convertView.getTag();
        }
       String headerText="";
       for (int i = 0; i < districts.size(); i++) {
		if (list.get(position).getDistrictId().equals(districts.get(i).getId())) {
			headerText=districts.get(i).getName();
		}
	}
       
        holder.text.setText(headerText);
        return convertView;
    }


	public long getHeaderId(int position) {
		// TODO Auto-generated method stub
		return Long.parseLong(list.get(position).getDistrictId());
	}

	@Override
	public int getPositionForSection(int arg0) {
		// TODO Auto-generated method stub
		 if (arg0 >= sectionIndices.length) {  
             arg0 = sectionIndices.length - 1;  
            } else if (arg0 < 0) {  
                arg0 = 0;  
            }  
            return sectionIndices[arg0];
	}

	@Override
	public int getSectionForPosition(int arg0) {
		// TODO Auto-generated method stub
		 for (int i = 0; i < sectionIndices.length; i++) {  
	            if (arg0 < sectionIndices[i]) {  
	                return i - 1;  
	            }  
	        }  
	        return sectionIndices.length - 1; 
	}

	@Override
	public Object[] getSections() {
		// TODO Auto-generated method stub
		return sectionHeaders;
	}
	 class HeaderViewHolder {
	        TextView text;
	    }

	   class ViewHolder {
	        TextView txtTitle;
	        TextView txtTejia;
	        TextView txtImax;
	        TextView txtMark;
	        TextView txtLocation;
	        TextView txtDistance;
	        TextView txtChangci;
	        TextView txtFuhao;
	        TextView txtPrice;
	        ImageView txtSeat;
	        ImageView txtQuan;
	        
	    }

}
